

const app = getApp()
Component({
  properties: {
    //属性值可以在组件使用时指定
    isCanDraw: {
      type: Boolean,
      value: false,
      observer(newVal, oldVal) {
        newVal && this.drawPic()
      }
    }
  },
  data: {
    imgDraw: {}, //绘制图片的大对象
    sharePath: '', //生成的分享图
    visible: false,
    IMGBASEURL: app.globalData.IMGBASEURL,
  },
  methods: {
    handleClose() {
      this.setData({
        visible: false
      })
      this.triggerEvent('close')
    },
    drawPic() {
      if (this.data.sharePath) { //如果已经绘制过了本地保存有图片不需要重新绘制
        this.setData({
          visible: true
        })
        this.triggerEvent('initData') 
        return
      }
      wx.showLoading({
        title: '生成中'
      })
      this.setData({
        imgDraw: {
          "width": "606rpx",
          "height": "1068rpx",
          "views": [
            {
              "type": "image",
              "url": `${this.data.IMGBASEURL}hb-bj.png`,
              "css": {
                "width": "606rpx",
                "height": "1068rpx",
              }
            },
          
            {
              "type": "image",
              "url": `${this.data.IMGBASEURL}tp.png`,
              "mode":"widthFix",
              "css": {
                "width":"554rpx",
                "height":"740rpx",
                "position": "absolute",
                "left": "26rpx",
                "top": "128rpx",
              }
            },
            { //底部二维码
              "type": "image",
              "mode": "auto",
              "url": `${this.data.IMGBASEURL}caotu-code.jpg`,
              "css": {
                "width": "102rpx",
                "height": "102rpx",
                "left": "26rpx",
                "top": "944rpx",
                "borderRadius":"8rpx"
              },
            },
          ]

        }
      })
    },
    onImgErr(e) {
      wx.hideLoading()
      wx.showToast({
        title: '生成分享图失败，请刷新页面重试'
      })
    },
    onImgOK(e) {
      wx.hideLoading()
      this.setData({
        sharePath: e.detail.path,
        visible: true,
      })
      wx.setStorageSync('cavas', e.detail.path)
      console.log(e.detail.path);
      this.data.sharePath = ""
     
      //通知外部绘制完成，重置isCanDraw为false
      this.triggerEvent('initData') 
    },
    preventDefault() { },
    // 保存图片
    handleSavePhoto() {
      wx.showLoading({
        title: '正在保存...',
        mask: true
      })
      wx.saveImageToPhotosAlbum({
        filePath: this.data.sharePath,
        success: () => {
          wx.showToast({
            title: '保存成功'
          })
          setTimeout(() => {
            this.setData({
              visible: false
            })
            this.triggerEvent('close')
          }, 300)
        },
        fail: () => {
          wx.getSetting({
            success: res => {
              let authSetting = res.authSetting
              if (!authSetting['scope.writePhotosAlbum']) {
                wx.showModal({
                  title: '提示',
                  content: '您未开启保存图片到相册的权限，请点击确定去开启权限！',
                  success(res) {
                    if (res.confirm) {
                      wx.openSetting()
                    }
                  }
                })
              }
            }
          })
          setTimeout(() => {
            wx.hideLoading()
            this.setData({
              visible: false
            })
            this.triggerEvent('close')
          }, 300)
        }
      })
    }
  },

})
